package com.mornd.gulimall.seckill.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.mornd.gulimall.apis.product.IProductClient;
import com.mornd.gulimall.apis.product.to.SkuInfoTo;
import com.mornd.gulimall.common.core.result.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author: mornd
 * @dateTime: 2023/8/31 - 10:26
 */

@Slf4j
@RestController
@RequestMapping("/test")
public class TestCOntroller {
    @Resource
    private IProductClient productClient;

    @GetMapping("/1")
    public Object test1() {
        log.info("sentinelTest()被调用了...");
        return "ok";
    }
    /*
        @SentinelResource 抛出BlockException或者UndeclaredThrowableException
     */
    @SentinelResource(value = "sentinelTest", blockHandler = "blockHandle")
    @GetMapping
    public Object sentinelTest() {
        log.info("sentinelTest()被调用了...");
        return "ok";
    }

    public Object blockHandle(BlockException e) {
        log.error("sentinelTest请求被阻塞了，", e.getMessage());
        return R.fail("sentinelTest请求被阻塞了");
    }

    @GetMapping("/feign/{id}")
    public Object feignTest(@PathVariable Long id) throws InterruptedException {
        Thread.sleep(200);
        R<SkuInfoTo> r = productClient.getSkuInfoById(id);
        return r;
    }
}
